package Client;

import util.ClientScanner;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 客户端的基本操作
 */
public class ClientInitClose {
    private Socket s;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    public ObjectInputStream getOis() {
        return ois;
    }

    public void setOis(ObjectInputStream ois) {
        this.ois = ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    public void setOos(ObjectOutputStream oos) {
        this.oos = oos;
    }

    /**
     * 客户端的初始化操作
     */
    public void clientInit(){
        try {
            // 1.提供客户端的通信地址和端口号
            s = new Socket(InetAddress.getLocalHost(),8888);
            System.out.println("客户端连接服务器成功!");
            // 2.使用输入输出流通信
            oos = new ObjectOutputStream(s.getOutputStream());
            ois = new ObjectInputStream(s.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 服务器关闭操作
     */
    public void clientClose(){
        try {
            ois.close();
            //oos.close();
            s.close();
            ClientScanner.scannerClose();
            System.out.println("客户端关闭成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
